/**
 * @author scylla
 * @version V1.0
 * @Project echo-java-base
 * @Title 使用线程异常处理类捕获异常
 * @Description *
 * @Package concurrent.mthread.exceptions
 * @Since 2021/8/3 11:18 上午
 */
package concurrent.mthread.exceptions;

import java.util.concurrent.ThreadFactory;

public class ExceptionThread2 implements Runnable {

    @Override
    public void run() {
        Thread t = Thread.currentThread();
        System.out.println("run() by" + t);
        System.out.println("eh = " + t.getUncaughtExceptionHandler());

        throw new RuntimeException("主动模拟异常。");
    }
}

/**
 * @author chentl
 * @version V1.0
 * @project bigdata-cloud
 * @title MyUncaughtExceptionHandler
 * @description 自定义线程异常处理类
 * @package concurrent.mthread.exceptions
 * @date 2021/8/3 11:20 上午
 * @copyright: 武汉大数据产业发展有限公司 All rights reserved.
 */
class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        System.err.println("caught: " + e);
    }
}

class HandlerThreadFactory implements ThreadFactory {

    @Override
    public Thread newThread(Runnable r) {
        System.out.println(this + "creating new Thread");
        Thread t = new Thread(r);

        System.out.println("created " + t);
        t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());

        System.out.println("eh = " + t.getUncaughtExceptionHandler());


        return t;
    }
}


